package com.jrx.anytxn.transaction.controller.cms;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.dto.billCompare.TlThirdBillDetailRes;
import com.jrx.anytxn.param.service.billCompare.ITlThirdBillDetailService;
import com.jrx.anytxn.transaction.bean.DealBillDetailBean;
import com.jrx.anytxn.transaction.service.IMistakeBillDetailService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author llk
 * @Date 10:54 上午 星期六 2021/7/17
 **/

@Api(tags = "CMS-对账相关API")
@RestController
@RequestMapping(value = "/transaction/bill")
public class BillDetilController {

    private static final Logger logger = LoggerFactory.getLogger(BillDetilController.class);

    @Autowired
    private InfoService infoService;

    @Autowired
    private IMistakeBillDetailService mistakeBillDetailService;

    @ApiOperation("分页查询对账差异表信息")
    @GetMapping("/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true)})
    public TxnRespResult<TxnPage<TlThirdBillDetailRes>> getAllGlInterfaceInfoByPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) {
        logger.debug("分页查询对账差异表信息：开始");
        TxnRespResult<TxnPage<TlThirdBillDetailRes>> result = new TxnRespResult<>();
        try {
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            result.getSuccess(mistakeBillDetailService.findAllPageByTenantId(pageNum, pageSize, tenantId));
            logger.debug("分页查询对账差异表信息：结束");
        } catch (TxnException e) {
            logger.error("分页查询对账差异表信息:{}", e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation(value = "处理差异记录", notes = "处理差异记录")
    @PutMapping("/")
    public TxnRespResult<Boolean> editSystemParameter(@RequestBody DealBillDetailBean dealBillDetailBean) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        try {
            String nickName = infoService.getUser().getNickName();
            mistakeBillDetailService.dealMistakeRecord(dealBillDetailBean,nickName);
            result.getSuccess(true);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result.getFail(TxnRespCode.ERROR.getCode(), TxnRespCode.ERROR.getUserTips());
        }
        return result;
    }

}
